package com.dhecp.project.common.dealdata;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.project.system.domain.SysModuleSet;
import com.dhecp.project.system.service.SysModuleSetService;

/**
 * 处理infoMap对象
 * @author DH-Xxj
 * @version 2019年12月16日
 */
@Service
public class InfoMapData {

    @Autowired
    private SysModuleSetService moduleSetService;
    
	/**
	 * 处理infoMap对象
	 * @param infoMap 必要参数：非网格字段（包括所有显示和隐藏的字段），json格式字符串
	 * @return Map
	 * @author DH-Xxj
	 */
	public Map<String, Object> infoMapDeal(String infoMapStr, String moduleTable){
		Map<String, Object> infoMap = TypeUtils.jsonToMap(infoMapStr);
		Boolean enableChange = true;//单据是否允许修改
		int enableChangeDays = 0;	//单据允许修改最大天数
		String enableChangemsg = "";
		if (infoMap.get("createTime") == null || "".equals(infoMap.get("createTime"))){
			Date currentTime = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String createTime = formatter.format(currentTime);
			infoMap.put("createTime", createTime);
		}else{
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date createTime = null;
			try {
				createTime = formatter.parse(infoMap.get("createTime").toString());
				int days = (int) ((new Date().getTime() - createTime.getTime()) / (1000*3600*24));
				SysModuleSet moduleSet = moduleSetService.queryModuleSetByModuleTable(moduleTable);
				if (moduleSet != null && moduleSet.getModuleChangeTimeLimit() != null && moduleSet.getModuleChangeTimeLimit() > 0){
					enableChangeDays = moduleSet.getModuleChangeTimeLimit();
				}
				if (days >= enableChangeDays){
					enableChange = false;
					enableChangemsg = MessageUtils.message("module.error.outlimttime",enableChangeDays);
				}
			} catch (ParseException e) {
				createTime = new Date();
			}
		}
		infoMap.put("enableChange", enableChange);
		infoMap.put("enableChangemsg", enableChangemsg);
		return infoMap;
	}
}
